<!--
* @author luguoxiang
* @date 2021-09-07
* 开源项目：https://gitee.com/lgx1992/lg-soar 求star！请给我star！请帮我点个star！
-->
<template>
  <t-dialog v-model:visible="visible" v-bind="$attrs" class="lg-modal" @close="close" @confirm="confirm">
    <template v-for="(x, key) in $slots" #[key]="scope">
      <slot :name="key" v-bind="scope" :data="data"></slot>
    </template>
  </t-dialog>
  <span @click="visible = true">
    <slot name="trigger"></slot>
  </span>
</template>

<script lang="ts" setup="EditContainerModal">
import { ref } from 'vue';
import { deepCopy } from '@/soar';

const emits = defineEmits(['open', 'close', 'confirm']);

const visible = ref<boolean>(false);
const data = ref<any>();

function open(params: any) {
  params = JSON.parse(JSON.stringify(params ?? ''));
  visible.value = true;
  data.value = params;
  emits('open', params);
}

function close() {
  visible.value = false;
  data.value = null;
  emits('close');
}

function confirm(e) {
  emits('confirm', {
    e,
    data: deepCopy(data.value),
    close,
  });
}

defineExpose({
  open,
  close,
});
</script>
<script lang="ts">
export default {
  name: 'LgModal',
};
</script>
